package com.repair.dao.fualtleixing.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;




import com.repair.dao.fualtleixing.IFaultLeixingDao;
import com.repair.domain.FaultLeixing;
import com.repair.factory.HibernateSessionFactory;





public class FaultLeixingDao implements IFaultLeixingDao {

	@Override
	public void saveFaultLeixing(FaultLeixing obj) {
		Session session = HibernateSessionFactory.getSession();
		
		try{
			Transaction tx = session.beginTransaction();
			if(session != null){
				session.saveOrUpdate(obj);
			    tx.commit();
				session.flush();
				session.clear();
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			if(session != null){
				session.flush();
				session.clear();
			}
		}
	}
	@Override
	@SuppressWarnings("unchecked")
	public List<FaultLeixing> getAllFaultLeixing(){
		List<FaultLeixing> list = new ArrayList<FaultLeixing>();
		Session session = HibernateSessionFactory.getSession();
		try{
			if(session != null){
				String sql = "select * from FAULT_LEIXING";
				SQLQuery query = session.createSQLQuery(sql);
				list = query.addEntity(FaultLeixing.class).list();
			}
		}
		catch(Exception e){
			
		}
		return list;
	}
	@Override
	public void deleteFaultLeixing(FaultLeixing obj) {
		Session session = HibernateSessionFactory.getSession();
		try {
			Transaction tx = session.beginTransaction();
			if (session != null) {
				 session.delete(obj);
				tx.commit();
			}
		} catch (Exception e) {

		} finally {
			if (session != null) {
				session.flush();
				session.clear();
			}
		}

	}
}
